<?php

/**
 * User:ayou chow
 * Date: 2019-12-09
 * Time: 09:40
 */

namespace app\admin\controller;


use app\common\model\LiveCategoryTopModel;
use app\common\model\UserModel;
use app\common\model\UserRecModel;
use think\Db;
use think\facade\Request;

class UserrecController extends BaseController
{
    public function index()
    {
        return $this->fetch();
    }

    public function add()
    {
        // 直播分类组合
        $category_list = LiveCategoryTopModel::with('nextCate')->where('status', 1)->order('sort ASC')->select();
        $this->assign('category_list', $category_list);
        return $this->fetch();
    }

    public function getlist()
    {
        $where        = [];
        $whereTimeEnd = '';

        $uid      = Request::param('uid');
        $end_time = Request::param("end_time");
        $status   = Request::param("status");

        if ($uid) {
            $where["uid"] = $uid;
        }
        if ($status != null) {
            switch ($status) {
                case 0:
                    $where["start_status"] = 0; //未生效
                    break;
                case 1:
                    $where["start_status"] = 1; //已生效
                    $where["end_status"]   = 0;
                    break;
                case 2:
                    $where["end_status"] = 1; //已过期
                    break;
            }
        }

        if ($end_time) {
            $whereTimeEnd = "end_time >= '{$end_time}'";
        }
        $page  = Request::param("page");
        $limit = Request::param("limit");
        $rec   = UserRecModel::where($where)->where($whereTimeEnd)->withJoin(['user' => function ($query) {
            $query->withField('nick_name, rec_weight');
        }])->limit(($page - 1) * $limit, $limit)->order("start_time", "desc")->select();
        if (count($rec) > 0) {
            $count = UserRecModel::where($where)->where($whereTimeEnd)->count("id");
            return json(["code" => 0, "msg" => "", "data" => $rec, "count" => $count]);
        }
        return self::bulidFail("未查询到相关数据");
    }

    public function add_post()
    {
        $uid        = Request::param("uid");
        $rec_weight = Request::param("rec_weight");
        $start      = Request::param("start_time");
        $end        = Request::param("end_time");
        // 直播细分分类
        $live_category_id = Request::param("live_category_id");

        $recModel                   = new UserRecModel();
        $recModel->uid              = $uid;
        $recModel->rec_weight       = $rec_weight;
        $recModel->start_time       = $start ? $start : date("Y-m-d H:i:s");
        $recModel->end_time         = $end;
        $recModel->live_category_id = $live_category_id;

        $userModel = UserModel::get($uid);
        if (!$userModel) {
            return self::bulidFail("用户不存在");
        }

        if (!$start) {
            $recModel->start_status = 1;

            Db::startTrans();

            if ($userModel->save(["rec_weight" => $rec_weight])) {
                if ($recModel->save()) {
                    Db::commit();

                    // 记录日志
                    $log_content = "添加主播({$uid})推荐";
                    parent::addAdminLog($log_content, $uid);

                    return self::bulidSuccess([]);
                } else {
                    Db::rollback();
                    return self::bulidFail();
                }
            } else {
                Db::rollback();
                return self::bulidFail();
            }
        } else {
            if ($recModel->save()) {
                // 记录日志
                $log_content = "添加主播({$uid})推荐";
                parent::addAdminLog($log_content, $uid);

                return self::bulidSuccess([]);
            }
            return self::bulidFail();
        }
    }

    public function start_post()
    {
        $id       = Request::param("id");
        $recModel = UserRecModel::get($id);

        if (!$recModel) {
            return self::bulidFail("所选推荐不存在");
        }
        // if ($recModel->start_status != 0){
        //     return self::bulidFail("所选任务状态异常");
        // }

        $userModel = UserModel::get($recModel->uid);
        if (!$userModel) {
            return self::bulidFail("用户不存在");
        }

        if ($userModel->save(["rec_weight" => $recModel->rec_weight])) {
            if ($recModel->save(['start_status' => 1, 'end_status' => 0])) {
                Db::commit();
                // 记录日志
                $log_content = "将用户" . $userModel->nick_name . "(" . $userModel->id . ")" . "的推荐ID：" . $recModel->id . "，状态修改为已生效";
                parent::addAdminLog($log_content, $userModel->id);

                return self::bulidSuccess([]);
            } else {
                Db::rollback();
                return self::bulidFail();
            }
        } else {
            Db::rollback();
            return self::bulidFail();
        }
    }

    public function end_post()
    {
        $id       = Request::param("id");
        $recModel = UserRecModel::get($id);

        if (!$recModel) {
            return self::bulidFail("所选任务不存在");
        }
        if ($recModel->end_status == 1) {
            return self::bulidFail("所选任务状态异常");
        }

        if ($recModel->start_status != 1) {
            return self::bulidFail("所选任务尚未生效");
        }

        $userModel = UserModel::get($recModel->uid);
        if (!$userModel) {
            return self::bulidFail("用户不存在");
        }

        if ($userModel->save(["rec_weight" => 0])) {
            if ($recModel->save(['end_status' => 1])) {
                Db::commit();
                // 记录日志
                $log_content = "将用户" . $userModel->nick_name . "(" . $userModel->id . ")" . "的推荐ID：" . $recModel->id . "，状态修改为已结束";
                parent::addAdminLog($log_content, $userModel->id);

                return self::bulidSuccess([]);
            } else {
                Db::rollback();
                return self::bulidFail();
            }
        } else {
            Db::rollback();
            return self::bulidFail();
        }
    }

    public function del_post()
    {
        $id        = Request::param("id");
        $rec_model = UserRecModel::get($id);

        if (!$rec_model) {
            return self::bulidFail("所选推荐不存在");
        }

        if (UserRecModel::where(['id' => $id])->delete()) {
            // 记录日志
            $log_content = "将用户(" . $rec_model->uid . ")主播推荐删除";
            parent::addAdminLog($log_content, $rec_model->uid);

            return self::bulidSuccess();
        }
        return self::bulidFail();
    }
}
